import cv2 as cv
import numpy as np

WINDOW_NAME = 'RGB Picker'


def create_callback(state: dict, key: str):
    return lambda value: state.update({key: value})


def setup_interface(state: dict):
    cv.namedWindow(WINDOW_NAME)

    cv.createTrackbar('R (0-255)', WINDOW_NAME, state['R'], 255, create_callback(state, 'R'))
    cv.createTrackbar('G (0-255)', WINDOW_NAME, state['G'], 255, create_callback(state, 'G'))
    cv.createTrackbar('B (0-255)', WINDOW_NAME, state['B'], 255, create_callback(state, 'B'))
    cv.createTrackbar('OFF/ON', WINDOW_NAME, state['switch'], 1, create_callback(state, 'switch'))


def update_image(img: np.ndarray, state: dict):
    if state['switch']:
        img[:] = [state['B'], state['G'], state['R']]
    else:
        img[:] = 0


def main():
    state = {'R': 0, 'G': 0, 'B': 0, 'switch': 1}
    img = np.zeros((300, 512, 3), dtype=np.uint8)

    setup_interface(state)

    while True:
        update_image(img, state)
        cv.imshow(WINDOW_NAME, img)

        if cv.waitKey(1) == 27:
            break

    cv.destroyAllWindows()


if __name__ == '__main__':
    main()
